package com.qf.dao.impl;

import com.qf.dao.AreaDao;
import com.qf.pojo.Area;
import com.qf.utils.DBTools;
import com.qf.utils.SQL;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class AreaDaoImpl implements AreaDao {
    private QueryRunner qr= new QueryRunner(DBTools.getDataSource());


    public List<Area> getArea(Integer type, Integer id) {
        BeanListHandler<Area> bean=new BeanListHandler<Area>(Area.class);
        String sql=null;
        if(type==1){
            sql="SELECT "+ SQL.AREA +"FROM area WHERE a_type=1";
            try {
                return qr.query(sql,bean);
            } catch (SQLException e) {
                e.printStackTrace();
             }
        }else{
            sql="SELECT "+ SQL.AREA +"FROM area WHERE a_type=? AND a_parent= ?";
            try {
                return qr.query(sql,bean,type,id);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public List<Area> selectOne(Integer id) {
        BeanListHandler<Area> bean=new BeanListHandler<Area>(Area.class);
        String sql=null;
        sql="SELECT "+ SQL.AREA +"FROM area WHERE a_id=?";
        try {
            return qr.query(sql,bean,id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
